package com.nulldev.util.java.swing.extSwing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.extSwing.resources.eMessageType;
import com.nulldev.util.string.StringsUtil;
import com.nulldev.util.threading.legacy.ThreadExecutor;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class eAdvancedMessage extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JLabel lblExtswingemessagetitle;
	private JTextArea txtrExtswingmessage;
	private boolean pressed;
	private Runnable r = new Runnable() {

		@Override
		public void run() {
			while (true) {
				if (pressed) {
					setAlwaysOnTop(false);
					setVisible(false);
					dispose();
					if (close) {
						System.exit(-1);
					}
					break;
				}
				try {
					Thread.sleep(SwingUtil.FPS_30);
				} catch (InterruptedException e) {
				}
			}
		}
	};

	/**
	 * Launch the application.
	 * 
	 * @return
	 */
	public static eAdvancedMessage main(final eMessageType type) {
		final eAdvancedMessage dialog = new eAdvancedMessage(type);
		ThemeApplier.applyTheme(dialog);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setLocation(
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()
						- dialog.getWidth()) / 2,
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight()
						- dialog.getHeight()) / 2);
		return dialog;
	}

	/**
	 * Create the dialog.
	 */
	public eAdvancedMessage(final eMessageType type) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				pressed = true;
			}
		});
		if (ThemeApplier.useSystem) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e) {
			}
		}

		setBounds(100, 100, 384, 159);
		setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		setTitle("extSwing.eMessage");
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		JButton btnOk = new JButton("OK");
		btnOk.setFocusable(false);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pressed = true;
			}
		});

		lblExtswingemessagetitle = new JLabel("extSwing.eMessage.title");

		txtrExtswingmessage = new JTextArea();
		txtrExtswingmessage.setText("extSwing.message");
		txtrExtswingmessage.setEditable(false);
		txtrExtswingmessage.setFocusable(false);
		txtrExtswingmessage.setColumns(16);

		JLabel label = new JLabel(" ");
		label.setIcon(type.toIcon());
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(6)
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblExtswingemessagetitle, GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
								.addComponent(txtrExtswingmessage, GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE))
							.addGap(0))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addContainerGap(306, Short.MAX_VALUE)
							.addComponent(btnOk)))
					.addGap(12))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(lblExtswingemessagetitle)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtrExtswingmessage, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
							.addComponent(btnOk)
							.addGap(10))))
		);
		contentPanel.setLayout(gl_contentPanel);
	}

	public void setMessageTitle(final String title) {
		this.lblExtswingemessagetitle.setText(title);
	}

	public void setWindowTitle(final String title) {
		this.setTitle(title);
	}

	public void setMessage(final String message) {
		//final int len = (int) ((this.txtrExtswingmessage.getFont().getSize() / 2.8) * message.length());
//		if (len > this.txtrExtswingmessage.getWidth()) {
//			final String[] xy = StringsUtil.splitEquallyA(message, 48);
//			// final String z = StringsUtil.toStringNL(xy);
//			this.txtrExtswingmessage.setText(message);
//			final int x = (int) (352 + (len * 0.7));
//			this.setSize(x, 159 + (xy.length * this.txtrExtswingmessage.getFont().getSize()));
//			setLocation(
//					(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
//							.getWidth() - x) / 2,
//					(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
//							.getHeight() - this.getHeight()) / 2);
//		} else {
//			this.txtrExtswingmessage.setText(message);
//			this.setSize(len, this.getHeight());
//		}
		final String[] xy = StringsUtil.splitEquallyA(message, 48);
		// final String z = StringsUtil.toStringNL(xy);
		this.txtrExtswingmessage.setText(message);
		//final int x = (int) ((len));
		//System.out.println(x + "," + ((xy.length * this.txtrExtswingmessage.getFont().getSize()) + 112));
//		this.setSize(
//				(x + (message.length() * 4)),
//				(message.split(JVMUtil.lineSep()).length * 12) + 156);
		final String[] nl = message.split(JVMUtil.lineSep());
		this.setSize(new Dimension(
				(int) ((message.length() > 24 ? (message.length() > 48 ? message.length() / 3.5 : message.length() / 1.5) : (message.length() * 1.1)) * this.txtrExtswingmessage.getFont().getSize2D()), 
				(nl.length == 0 ? 140 :  140 + (nl.length * 20))
				));
		this.setLocation(
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
						.getWidth() - this.getWidth()) / 2,
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
						.getHeight() - this.getHeight()) / 2);
		this.setVisible(true);
	}

	public void waitForOK() {
		setAlwaysOnTop(true);
		ThreadExecutor.execute(r);
	}

	private boolean close;
	public void closeOnFinish() {
		this.close = true;
	}
}
